package com.fh.Advice;

import com.alibaba.fastjson.JSON;
import com.fh.controller.base.BaseController;
import com.fh.model.OnlineCounter;
import com.fh.util.Const;
import com.fh.util.CoverUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Random;

/**
 * 创建者：shanlin
 * 创建时间：2017/10/25 15:01
 **/
@RestController
@ControllerAdvice
public class CommonController extends BaseController{

//	@ExceptionHandler(Exception.class)
//	public Object handleAllException(Exception ex) throws ClassNotFoundException, NoSuchMethodException {
//		StackTraceElement stackTraceElement =  Arrays.asList(ex.getStackTrace()).stream().findFirst().get();
//		Class classObj = Class.forName(stackTraceElement.getClassName());
//		Logger logger = LoggerFactory.getLogger(classObj);
//		String reType = classObj.getMethod(stackTraceElement.getMethodName()).getGenericReturnType().getTypeName();
//		logger.error(stackTraceElement.getMethodName() + "()方法异常，信息如下：",ex);
//		if(Objects.equals(ModelAndView.class.getName(),reType)){
//			ModelAndView mv = new ModelAndView("loantask/abcde");
//			mv.addObject("errMsg",ex);
//			return  mv;
//		}
//		return R.error(0,ex.getMessage());
//	}
	
	@ModelAttribute
	public void addGlobalAttributes(Model model) {
		//admin
		model.addAttribute("ADMIN",Const.ADMIN);
		//催收组长
		model.addAttribute("CSZZ",Const.ROLE_CSZZ);
		//催收员
		model.addAttribute("CSY",Const.ROLE_CSY);
		//催收经理
		model.addAttribute("CSJL",Const.ROLE_CSJL);
		//委外公司
		model.addAttribute("WWGS",Const.ROLE_WWGS);
		//委外专员
		model.addAttribute("WWZY",Const.ROLE_WWZY);
        //催收关系下拉框列表
        model.addAttribute("callshipmap", JSON.toJSON(Const.CALLSHIPMAP));
        //催收结果下拉框列表(新增)
        model.addAttribute("callresulttypen",  Const.CALLRESULTMAPN);
        //催收结果下拉框列表(查询)
        model.addAttribute("callresulttypef", JSON.toJSON(Const.CALLRESULTMAPF));
        //联系人关系下拉列表
        model.addAttribute("linkManShipMap", JSON.toJSON(Const.linkManShip));
		//清楚缓存  时间戳
		model.addAttribute("getTime", "?getTime=" + (new Date()).getTime());
		//手机号加密 状态
		model.addAttribute("isCover", CoverUtil.isCoverGet(2));
		//逾期阶段
        model.addAttribute("overduePhase", JSON.toJSON(Const.overduePhase));
        //在线人数统计
        model.addAttribute("cnt", OnlineCounter.getOnline());
        model.addAttribute("ipList", String.join("|",OnlineCounter.getIpList()));
        //随机整数
		model.addAttribute("randomVal",getIntRandom(1,3));
	}
	
	private int getIntRandom(int min,int max){
		Random random = new Random();
		int s = random.nextInt(max)%(max-min+1) + min;
		return s;
	}
}
